// 校验是否外部链接
export function isExternal(path: string){
    return /^(https?:|mailto:|tel:)/.test(path);
};

/**
 * 中划线字符驼峰
 * @param {*} str 要转换的字符串
 * @returns 返回值
 */
export function toHump(str: string){
    if (!str) return str;
    // 匹配-后面的第一个字符，变成大写
    return str.replace(/-(\w)/g, function (all, letter) {
      return letter.toUpperCase();
      // 匹配以任何空白符和字母开头的第一个字面，变成大写
      }) .replace(/(\s|^)[a-z]/g, function (char) {
        return char.toUpperCase();
      });
}

/**
 * 处理含有子父关系的数据
 * @param {*} data 
 * @param {*} pid 
 * @param {*} id 
 * @returns 返回值
 */
export function toTreeData(data, pid="parentid", id="id") {

  const map = new Map();
  const roots = [];
  
  // 创建映射
  data.forEach(item => {
    map.set(item[id], { ...item, children: [] });
  });
  
  // 构建树
  data.forEach(item => {
    const parent = map.get(item[pid]);
    if (parent) {
      parent.children.push(map.get(item[id]));
    } else{
      roots.push(map.get(item[id]));
    }
  });
  
  return roots;  
}



// 修改和查看表格数据通用方法
export function alertEditAndLook(data:any[]) {
  if (data == null || data.length == 0) {
    ElMessage.warning("请选择要操作的数据")
    return false
  } else {
    if (data.length > 1) {
      ElMessage.warning("只能选择一条数据进行操作")
      return false
    } else {
      return true
    }
  }
}

// 删除的全局处理
export function removeData(data:any[], callback: any ){
  if (data.length == 0) {
    ElMessage.warning("请选择要删除的数据")
  } else {
    ElMessageBox.confirm(
        `${data.length}条数据将被删除，是否继续？`,
       "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        callback()
      }).catch(() => {
        ElMessage.info("取消删除")
      })
  }
}


//校验是否有权限
export function validPermission(permissionList: string[], toValidPermission: string){
  return permissionList.includes(toValidPermission) || permissionList.includes("*:*:*")
}

//生成uuid
export function generateUUID() {
  let timestamp = new Date().getTime();
  let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    let r = (timestamp + Math.random() * 16) % 16 | 0;
    timestamp = Math.floor(timestamp / 16);
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}

/**
 * 随机生成字符串
 * @returns {string}
 */
export function generateRandomString (count:number){
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  const charactersLength = characters.length;
  for (let i = 0; i < count; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
};

/**
 * 下载文件方法
 * @param data 
 * @param fileName 
 * @param mineType 
 */
export function download(data: Blob, fileName: string){
  // 创建 blob
  const blob = new Blob([data], { type: 'blob' })
  // 创建 href 超链接，点击进行下载
  window.URL = window.URL || window.webkitURL
  const href = URL.createObjectURL(blob)
  const downA = document.createElement('a')
  downA.href = href
  downA.download = fileName
  downA.click()
  // 销毁超连接
  window.URL.revokeObjectURL(href)
}